import axios, { AxiosProgressEvent } from "axios";

// 创建 axios 实例
const api = axios.create({
  // baseURL: "http://127.0.0.1:8000/jinx/", // 你的 API 地址
  baseURL: "http://124.222.225.192:8000/jinx/", // 你的 API 地址
  headers: {
    "Content-Type": "application/json",
  },
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 检查 localStorage 是否有 token
    const token = localStorage.getItem("token");
    // 如果存在 token 且请求需要 token（非登录请求），则在请求头中添加 Authorization
    if (token && config.url !== "/login") {
      config.headers["Authorization"] = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器 - 统一处理401等错误
api.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    if (error.response?.status === 401) {
      // Token过期或无效，清除本地存储并跳转到登录页
      localStorage.removeItem("token");
      localStorage.removeItem("user");

      // 如果当前不在登录页，则跳转
      if (window.location.pathname !== "/login" && window.location.pathname !== "/") {
        window.location.href = "/login";
      }
    }
    return Promise.reject(error);
  }
);

// 默认请求方法封装
export const postRequest = async (
  url: string,
  data: object,
  needsToken: boolean = false
) => {
  try {
    const response = await api.post(url, data, {
      headers: needsToken
        ? { Authorization: `Bearer ${localStorage.getItem("token")}` }
        : {},
    });
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const getRequest = async (
  url: string,
  params: object = {},
  needsToken: boolean = false
) => {
  try {
    const response = await api.get(url, {
      params,
      headers: needsToken
        ? { Authorization: `Bearer ${localStorage.getItem("token")}` }
        : {},
    });
    return response.data;
  } catch (error) {
    throw error;
  }
};

// 文件上传（multipart/form-data）
export const uploadRequest = async (
  url: string,
  formData: FormData,
  needsToken: boolean = false,
  onUploadProgress?: (progressEvent: AxiosProgressEvent) => void
) => {
  try {
    const response = await api.post(url, formData, {
      headers: {
        ...(needsToken ? { Authorization: `Bearer ${localStorage.getItem("token")}` } : {}),
        // 不手动设置 multipart 边界，交由浏览器处理
        'Content-Type': 'multipart/form-data',
      },
      onUploadProgress,
    });
    return response.data;
  } catch (error) {
    // 规范化错误信息，尽量提供服务端返回的 msg
    const anyErr: any = error as any;
    const status = anyErr?.response?.status;
    const serverData = anyErr?.response?.data;
    const serverMsg = serverData?.msg || serverData?.message || serverData?.error;
    if (status === 413) {
      throw new Error('文件大小超出限制，请上传不超过10MB的文件');
    }
    if (serverMsg) {
      throw new Error(serverMsg);
    }
    throw new Error(anyErr?.message || '上传失败');
  }
};
